#include"stack.h"

void STInit(ST* ps)

{
	assert(ps);
	ps->arr = NULL;
	ps->top = ps->capacity = 0;

}
void STDestroy(ST* ps)
{
	if (ps->arr != NULL)
	{
		free(ps->arr);

	}
	ps->arr = NULL;
	ps->top = ps->capacity = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newCapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!\n");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newCapacity;
		
	}
	ps->arr[ps->top++] = x;
	
}
bool STEmpty(ST* ps)
{
	assert(ps);
	return ps->top == 0;
}

void STPop(ST* ps)
{
	assert(!STEmpty(ps));
	--ps->top;
}

STDataType STTop(ST* ps)
{
	assert(!STEmpty(ps));
	return ps->arr[ps->top-1];
}

int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}